<?php

namespace app\index\controller;
use think\Exception;
use think\Request;
use think\Db;
use app\common\model\VisitLog;

class Journal extends Base
{
	public function index(){
		// $data = VisitLog::with('nodes')->select();
		$request = Request::instance();
        
        if($request->isAjax()){
        	$post = $request->post();

            if( !empty($post['datemin']) && !empty($post['datemax']) ){
                $where['create_time'] = ['between time',[$post['datemin'],$post['datemax']]];                  
                    
            }
            if ( empty($post['datemin']) && !empty($post['datemax']) ){             
                $where['create_time'] = ['<= time',$post['datemax']];
                    
                    
            }
            if ( !empty($post['datemin']) && empty($post['datemax']) ) {
                $where['create_time'] = ['> time',$post['datemin']];
                 
            }
            if( empty($where) ){
                $where = '1=1';
            }  

            $model= VisitLog::with('nodes,admin')->where($where)
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);
            if(count($model)>=1){
                foreach ($model as $key => $value) {
                    $value['nick_name'] = $value->admin['nick_name'];
                    $value['node_name'] = $value->nodes['node_name'];
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                $res['data'] = '';
                $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }

           
            return $res;

        }else{
        	return $this->fetch(); 
        }
	}
}

